<?php

/**
 * This is the model class for table "{{page}}".
 *
 * The followings are the available columns in table '{{page}}':
 * @property integer $id
 * @property string $name
 * @property string $title
 * @property string $description
 * @property string $keywords
 * @property string $url
 * @property integer $is_active
 * @property integer $status
 */
class Page extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Page the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{page}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, title, description, keywords, url, status', 'required'),
            array('is_active, status', 'numerical', 'integerOnly' => true),
            array('name, title, description, keywords, url,', 'length', 'max' => 254),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id,name, title, description, keywords, url, is_active, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Название',
            'title' => 'Заголовок стрвницы',
            'description' => 'Описание страницы',
            'keywords' => 'Ключевые слова',
            'url' => 'Алиас',
            'is_active' => 'Активность',
            'status' => 'Статус',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('keywords', $this->keywords, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('is_active', $this->is_active);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function isActive() {
        return $this->is_active ? 'Активна' : 'Не aктивна';
    }

    //получить тип страницы
    public static function getTypePage($type = 'static') {
        switch ($type) {
            case 'static':
                $typeId = 0;
                break;
            case 'flat':
                $typeId = 1;
                break;
            case 'city':
                $typeId = 2;
                break;
            case 'info':
                $typeId = 3;
                break;
            case 'user':
                $typeId = 4;
                break;
            default:
                $typeId = 0;
                break;
        }

        return $typeId;
    }

    public function getPageVsType($type = 'static') {
        return Page::model()->findAll('status=:status', array(':status' => Page::getTypePage($type)));
    }

    public function getCountPageVsType($type = 'static') {
        return Page::model()->count('status=:status', array(':status' => Page::getTypePage($type)));
    }
    
    public static function getPageFromUrl($url) {
        return Page::model()->find('url=:url', array(':url' => $url));
    }

    public function addPage($attributes) {
        $page = new Page;
        $page->attributes = $attributes;
        if ($page->save()) {
            return $page->id;
        } else {
            return false;
        }
    }

    // удаляет страницу
    public function deletePage($id) {
        if (!empty($id)) {
            $model = Page::model()->findByPk($id);
            if ($model->delete()) {
                return true;
            }
        }

        return false;
    }

}